/*
 * MainMod.c
 *
 *  Created on: 2012-02-10
 *      Author: mxw
 */
#include <MainModu.h>
#include <timer.h>
#include <commlib.h>
#include <ledDrv.h>
#include <Console.h>

#define MAIN_MOD_BUFF_LEN  8192

CModule gMainMod;
static char _main_mod_buf[MAIN_MOD_BUFF_LEN] = {0};


static int on_main_mod_recv_msg(struct _cmodule_* thes, CMsg* msg) {
    switch(msg->msg_id) {
    case CMD_TIMER_TIMEOUT:
        call_timer_func(msg->data[0]);
        return 0;
    case CMD_KEY_DOWN:
        printf("key:%d was pressed.\r\n", msg->data[0]);
        return 0;
    }
    printf("[Err] main module recv msg:0x%x, data len:%d, data[0]=%c\r\n", msg->msg_id, msg->data_len, msg->data[0]);    
    return 0;
}

static void on_main_timer_test(unsigned short tid) {
    if(LED_ON == get_led(LED_1)) {
        set_led(LED_1, LED_OFF);
    } else {
        set_led(LED_1, LED_ON);
    }
    //printf("time_id=%d, timeout notify!\r\n", tid);
}


static int main_mod_sub_init() {
    unsigned short tid = 0;

    printf("__\r\n");
    //printf("hello world!  ->【Alphabet OK】\r\n");
    set_led(LED_2, LED_ON);
    //printf("你好!好 ->【中文OK】\r\n");
    //printf("こんにちは ->【日本語OK】\r\n");

    tid = get_new_timer(on_main_timer_test);
    if (ERR_TIMER_ID == tid) {
        printf("get test_Timer Failed!\r\n");
    }
    start_timer(tid, 2000); //0.5s timer
    set_led(LED_ALL, LED_OFF);

    show_main_help();
    return 0;
}


int init_main_module() {
    construct_module(&gMainMod, "main module", on_main_mod_recv_msg);
    gMainMod.q.set_queue_buf(&gMainMod.q, _main_mod_buf, MAIN_MOD_BUFF_LEN);
    gMainMod.run(&gMainMod);
    main_mod_sub_init();
    return 0;
}

